﻿using InTheHand.Net;
using MyTRCP.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MyTRCP.Client
{
    /// <summary>
    /// DevMgmtWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DevMgmtWindow : Window
    {
        private readonly List<BluetoothAddress> _macList;
        private readonly Func<Task<IReadOnlyCollection<BtDevInfo>?>> _scanFunc;
        public List<BluetoothAddress> MacList { get { return _macList; } }
        public DevMgmtWindow(List<BluetoothAddress> originalMacList, Func<Task<IReadOnlyCollection<BtDevInfo>?>> scanFunc)
        {
            InitializeComponent();
            _macList = new List<BluetoothAddress>([.. originalMacList]);
            _scanFunc = scanFunc;
            lb_macs.ItemStringFormat = "C";
            lb_macs.ItemsSource = _macList;
        }

        private void b_ok_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void b_cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void b_modify_Click(object sender, RoutedEventArgs e)
        {
            if (lb_macs.SelectedItem != null && lb_macs.SelectedItem is BluetoothAddress ba)
            {
                EditMacAddrWindow emaw = new EditMacAddrWindow(ba);
                if (emaw.ShowDialog() == true)
                {
                    if (!_macList.Exists(c => c == emaw.NewAddr))
                    {
                        int index = lb_macs.SelectedIndex;
                        lb_macs.ItemsSource = null;
                        _macList.RemoveAt(index);
                        _macList.Insert(index, emaw.NewAddr);
                        lb_macs.ItemsSource = _macList;
                        lb_macs.SelectedIndex = index;
                    }
                    else
                    {
                        MessageBox.Show("修改后的MAC已存在，请重新修改或删除");
                    }
                }
            }
        }

        private void b_manadd_Click(object sender, RoutedEventArgs e)
        {
            EditMacAddrWindow emaw = new EditMacAddrWindow();
            if (emaw.ShowDialog() == true)
            {
                if (!_macList.Exists(c => c == emaw.NewAddr))
                {
                    int index = lb_macs.SelectedIndex;
                    lb_macs.ItemsSource = null;
                    _macList.Add(emaw.NewAddr);
                    lb_macs.ItemsSource = _macList;
                    lb_macs.SelectedIndex = index;
                }
                else
                {
                    MessageBox.Show("已添加此MAC，无需重复添加");
                }
            }
        }

        private void b_del_Click(object sender, RoutedEventArgs e)
        {
            if (lb_macs.SelectedItem != null && lb_macs.SelectedItem is BluetoothAddress ba)
            {
                if (MessageBox.Show($"确认删除{ba:C}？", "请确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    int index = lb_macs.SelectedIndex;
                    lb_macs.ItemsSource = null;
                    _macList.RemoveAt(index);
                    lb_macs.ItemsSource = _macList;
                }
            }
        }

        private void b_up_Click(object sender, RoutedEventArgs e)
        {
            if (lb_macs.SelectedItem != null)
            {
                int index = lb_macs.SelectedIndex;
                if (index > 0)
                {
                    lb_macs.ItemsSource = null;
                    var tmp = _macList[index];
                    _macList.RemoveAt(index);
                    _macList.Insert(index - 1, tmp);
                    lb_macs.ItemsSource = _macList;
                    lb_macs.SelectedIndex = index - 1;
                }
            }
        }

        private void b_down_Click(object sender, RoutedEventArgs e)
        {
            if (lb_macs.SelectedItem != null)
            {
                int index = lb_macs.SelectedIndex;
                if (index < lb_macs.Items.Count - 1)
                {
                    lb_macs.ItemsSource = null;
                    var tmp = _macList[index];
                    _macList.RemoveAt(index);
                    _macList.Insert(index + 1, tmp);
                    lb_macs.ItemsSource = _macList;
                    lb_macs.SelectedIndex = index + 1;
                }
            }
        }

        private void ListBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            b_modify_Click(sender, e);
        }

        private void b_scanadd_Click(object sender, RoutedEventArgs e)
        {
            ScanBTWindow sbtw = new ScanBTWindow(_scanFunc);
            if (sbtw.ShowDialog() == true)
            {
                if (sbtw.SelectedMac != null)
                {
                    if (!_macList.Exists(c => c == sbtw.SelectedMac))
                    {
                        int index = lb_macs.SelectedIndex;
                        lb_macs.ItemsSource = null;
                        _macList.Add(sbtw.SelectedMac);
                        lb_macs.ItemsSource = _macList;
                        lb_macs.SelectedIndex = index;
                    }
                    else
                    {
                        MessageBox.Show("已添加此MAC，无需重复添加");
                    }
                }
            }
        }
    }
}
